package com.qzh.summer.xml;
import com.qzh.summer.SummerException.SummerException;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;
import java.util.List;
/**
 * Created by lenovo on 2017-6-2.
 */
public class DOM4JTest {
  public void parseXML() throws SummerException {
    SAXReader saxReader = new SAXReader();
    try{
      Document document = saxReader.read(new File("src\\xml\\summer.xml"));
       //根元素，最外面的大括号
      Element rootElement = document.getRootElement();
       String name = rootElement.getName();
       if(!"summer".equals(name)){
         throw new SummerException("该配置文件无summer根元素");
       }
      Iterator<Element> it = rootElement.elementIterator();
      while (it.hasNext()){
        Element element = it.next();
        List<Attribute> attrs = element.attributes();
        for(Attribute attr: attrs){
          System.out.println("属性名：" + attr.getName() + "---- 属性值：" + attr.getValue() );
        }
        Iterator<Element> cit = element.elementIterator();
        while (cit.hasNext()){
          Element child = cit.next();
          System.out.println("子节点：" + child.getName());
        }
      }
    }catch (DocumentException e){
      e.printStackTrace();
    }
  }
  public static void main(String[] args) throws SummerException {
    DOM4JTest dom4JTest = new DOM4JTest();
    dom4JTest.parseXML();
  }
}